package com.qb.controller;

import com.qb.entity.DialogResult;
import com.qb.mapper.UserMapper;
import com.qb.pojo.User;
import com.qb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    //登录
    @RequestMapping("login")
    @ResponseBody
    public DialogResult login(HttpSession Session, String uname, String upass) {
        DialogResult result = new DialogResult();
        User u = new User();
        u.setUname(uname);
        u.setUpass(upass);
        User user = userService.login(u);
//        System.out.println(user);
        if (user != null) {
            Session.setAttribute("user", user);
            result.setMsg("登陆成功！");
//            System.out.println(result);
        } else {
            result.setError("请输入有效账户或密码!");
//            System.out.println(result);
        }
        return result;
    }

    //退出
    @RequestMapping("logout")
    @ResponseBody
    public void logout(HttpSession session) {
        session.invalidate();
    }

    //查询所有用户信息
    @RequestMapping("findAll")
    @ResponseBody
    public List<User> findAll() {
        return userService.findAll();
    }

    //删除用户信息
    @RequestMapping("deleteByUid")
    @ResponseBody
    public DialogResult deleteByUid(Integer uid) {
        DialogResult result = new DialogResult();
        boolean flag = userService.deleteByUid(uid);

        if (flag == true) {
            result.setMsg("删除成功！");
        } else {
            result.setError("删除失败!");
        }
        return result;
    }

    //模糊查询
    @RequestMapping("findByUname")
    @ResponseBody
    public List<User> findByUname(String uname) {
        return userService.findByUname(uname);

    }

    //详情
    @RequestMapping("findUser")
    @ResponseBody
    public Map<String, Object> findUser(Integer uid) {
        Map<String, Object> map = userService.findUser(uid);

        return map;
    }
}
